<?php
/**
 * 协程上下文
 *  协程退出后上下文自动清理
 *
 * @author		黑冰 <954650386@qq.com>
 */

namespace waf\context;

class WContext
{
    /**
     * 获取当前协程id
     *
     * @return mixed
     */
    public static function getCid()
    {
        return \Swoole\Coroutine::getCid();
    }

    /**
     * 获取当前协程的traceId，以协程为分隔粒度
     *
     * @return mixed
     */
    public static function getTraceId()
    {
        $context    = \Swoole\Coroutine::getContext();
        if(!isset($context['traceId']))
        {
            $context['traceId'] = \uniqid();
        }

        return $context['traceId'];
    }

    /**
     * 设置tarceId, rpc 远程调用时，可以串联起来
     *
     * @param string $tid
     */
    public static function setTraceId($tid = '')
    {
        $context    = \Swoole\Coroutine::getContext();
        if($tid)
        {
            $context['traceId'] = $tid;
        }else{
            $context['traceId'] = \uniqid();
        }
    }
}
?>
